var url = location.search; //获取url中"?"符后的字串
var kid, subId, userRole, score = 6, purposeScore = 1, scoreTwo = 3;
$(function(){
    if (url.indexOf("?") != -1) { 
        var str = decodeURI(url.substr(1)); 
        var theRequest = new Object(); 
        var strs = str.split("&"); 
        for(var i = 0; i < strs.length; i ++) { 
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); 
        } 
        kid = theRequest.kid;
        subId = theRequest.subId;
        var isScore = theRequest.isScore;
        userRole = theRequest.userRole;
        if(isScore == "是"){
            $('.hidden-score').hide();
        } else {
            $('.hidden-score').show();
        }
        if(userRole == '大众评委'){
            $('.hidden-score-two').hide();
            $('.click-score').eq(0).hide();
            $('.click-score').eq(1).show();
        } else {
            $('.hidden-score-two').show();
            $('.click-score').eq(0).show();
            $('.click-score').eq(1).hide();
        }
    }
    var tranData = {'kid': kid}
    getdata(host + "userproject/findDetailByKid", tranData,
        function(res){
            content = res.res.data;
            if(content != "" && content != null){
                $('.declareProject').html(content.declareProject);
                $('.isRegEnterprice').html(content.isRegEnterprice);
                if(content.isRegEnterprice == "是"){
                    $('.hiddenEnterpriceName').show();
                    $('.enterpriceName').html(content.enterpriceName);
                } else {
                    $('.hiddenEnterpriceName').hide();
                }
                $('.industryOwned').html(content.industryOwned);
                let attachmentPathNew = [];
                var attachmentPath = content.attachmentPath.split(',')
                for (var i = 0; i < attachmentPath.length; i++) {
                    attachmentPathNew[i] = interviewHost + attachmentPath[i];
                }
                var data = {
                    content: attachmentPathNew
                };
                var html = template('tlp', data);
                document.getElementById('topClickPreview').innerHTML = html;
                for (var i = 0; i < $('.topClickPreview').length; i++) {
                    var attachmentPathType = attachmentPathNew[i].substring(attachmentPathNew[i].lastIndexOf('.') + 1);
                    renderfileType(attachmentPathType, $('.topClickPreview').eq(i).parent());
                }
                $('.declarationMemo').html(content.declarationMemo);
                $('.fundInput').html(content.fundInput);
                var enterMembercert = content.enterMembercert;
                var enterMembercertType = enterMembercert.substring(enterMembercert.lastIndexOf('.') + 1);
                if(enterMembercert == "" || enterMembercert == null){
                    $('.clickPreviewBox').eq(0).hide();
                } else {
                    renderfileType(enterMembercertType, $('.clickPreview').eq(0).parent());
                    $('.clickPreview').eq(0).attr('url',interviewHost + content.enterMembercert);
                }
                var enterAuditreport = content.enterAuditreport;
                var enterAuditreportType = enterAuditreport.substring(enterAuditreport.lastIndexOf('.') + 1);
                if(enterAuditreport == "" || enterAuditreport == null){
                    $('.clickPreviewBox').eq(1).hide();
                } else {
                    renderfileType(enterAuditreportType, $('.clickPreview').eq(1).parent());
                    $('.clickPreview').eq(1).attr('url',interviewHost + content.enterAuditreport);
                }
                var enterLicense = content.enterLicense;
                var enterLicenseType = enterLicense.substring(enterLicense.lastIndexOf('.') + 1);
                if(enterLicense == "" || enterLicense == null){
                    $('.clickPreviewBox').eq(2).hide();
                } else {
                    renderfileType(enterLicenseType, $('.clickPreview').eq(2).parent());
                    $('.clickPreview').eq(2).attr('url',interviewHost + content.enterLicense);
                }
                var enterProductcert = content.enterProductcert;
                var enterProductcertType = enterProductcert.substring(enterProductcert.lastIndexOf('.') + 1);
                if(enterProductcert == "" || enterProductcert == null){
                    $('.clickPreviewBox').eq(3).hide();
                } else {
                    renderfileType(enterProductcertType, $('.clickPreview').eq(3).parent());
                    $('.clickPreview').eq(3).attr('url',interviewHost + content.enterProductcert);
                }
                var enterSupportTable = content.enterSupportTable;
                var enterSupportTableType = enterSupportTable.substring(enterSupportTable.lastIndexOf('.') + 1);
                if(enterSupportTable == "" || enterSupportTable == null){
                    $('.clickPreviewBox').eq(4).hide();
                } else {
                    renderfileType(enterSupportTableType, $('.clickPreview').eq(4).parent());
                    $('.clickPreview').eq(4).attr('url',interviewHost + content.enterSupportTable);
                }
                var enterTable = content.enterTable;
                var enterTableType = enterTable.substring(enterTable.lastIndexOf('.') + 1);
                if(enterTable == "" || enterTable == null){
                    $('.clickPreviewBox').eq(5).hide();
                } else {
                    renderfileType(enterTableType, $('.clickPreview').eq(5).parent());
                    $('.clickPreview').eq(5).attr('url',interviewHost + content.enterTable);
                }
            }
        }
    );
})
// 点击提交
$(document).on("click", ".click-submit", function (e) {
    if(userRole == "大众评委"){
        var scoreNew = scoreTwo; 
    } else{
        var scoreNew = score + purposeScore;
    }
    if(purposeScore == 2){
        var data = {'isTouzi': '是', 'score': scoreNew, 'id': subId}
    } else {
        var data = {'isTouzi': '否', 'score': scoreNew, 'id': subId}
    }
    getdata(host + "projectalloc/updateProjectScore", data,
        function(res){
            if(res.res.code == 40000){
                Common.TipTop($(".warning"), '您已评分成功' , "-20px");
                return false;
            } else {
                Common.TipTop($(".warning"), res.res.msg , "-20px");
                return false;
            }
        }
    );
})
// 点击评分事件
$(document).on("click", ".click-score p", function (e) {
    $(this).addClass('current');
    $(this).parent().siblings().children().removeClass('current');
    score = parseInt($(this).attr('name'));
    scoreTwo = parseInt($(this).attr('name'));
})
// 点击投资意向事件
$(document).on("click", ".invest-purpose p", function (e) {
    $(this).addClass('current');
    $(this).parent().siblings().children().removeClass('current');
    purposeScore = parseInt($(this).attr('name'));
    
})
// 点击预览
$(document).on("click", ".clickPreview,.topClickPreview", function (e) {
    // 下载文件
    var url = $(this).attr('url');
    var type = url.substring(url.lastIndexOf('.') + 1);
    var fileName = type.substring(type.lastIndexOf('\\') + 1);
    if(type == 'png' || type == 'jpg' || type == 'jpeg'){
        // 如果上传资料为图片，点击图片放大
        DisplayPicture(url);
    } else {
        // 下载word 文档 ppt 直接打开预览，无法下载
        //window.open(url)
        window.win = open (url); 
        setTimeout('win.document.execCommand("SaveAs")', 500); 
    }
    // 下载一
    //    var $aa = $('<a>');    
    //    $aa.attr('href',url);    
    //     window.open($aa.attr('href'), $aa.attr('target'));  $aa.get(0).click(); 
    // 下载三(html5)
    //    var a = document.createElement('a');
    //    var url = window.URL.createObjectURL(url);
    //    var filename = 'what-you-want';
    //    a.href = url;
    //    a.download = filename;
    //    a.click();
    //    window.URL.revokeObjectURL(url);
})

// 点击图片放大函数   val 传递的图片路径
function DisplayPicture(val) {
    if (val != 'null' && typeof (val) != "undefined" && val != 0 && val != null) {
        var json = {
            "title": "", // 相册标题
            "id": 1, // 相册id
            "data": [   // 相册包含的图片，数组格式
                {
                "alt": "图片名",
                "src": val, // 原图地址
                },
            ],
        };
        layer.photos({
            photos: json,
            moveType: 1,
            closeBtn: [0, true],
        });
    } else {
        $.messager.alert('提示', '该图片不存在', 'error');
    }
}
